package pishen;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Pair;

// Assortativity
public class AssortativityCalculator<V , E> {
	private Graph<V, E> g;
	
	// constructor
	public AssortativityCalculator(Graph<V, E> g){
		this.g = g; 
	}
	
	// main calculate process
	public double run(){
		
		double total_degree = 0 ;
		for(V v : g.getVertices()){
        	total_degree += g.degree(v);
        }
		

		double edges_count = g.getEdgeCount(); 
		

		double num1  = 0 , num2 = 0 , den = 0; 
		for (E e : g.getEdges()){
			Pair<V> p = g.getEndpoints(e);
			V v1 = p.getFirst();
			V v2 = p.getSecond();
			num1 += g.degree(v1)*g.degree(v2);
			num2 += (g.degree(v1)+g.degree(v2));
			den += (g.degree(v1)*g.degree(v1)+g.degree(v2)*g.degree(v2));
		}
		num1 /= edges_count;
		num2 = (num2/(2 * edges_count)) * (num2/(2 * edges_count));
		den /= (2 * edges_count);
		

		return (num1-num2)/(den-num2);
	}	
}